<template>
	<view class="page page-bottom-bar form-edit">
		<view class="edit-box">
			<view class="item">
				<view class="label">姓名</view>
				<input :value="form.name" placeholder="输入联系人姓名" @input="Onname" placeholder-class="placeholderClass" />
			</view>
			<view class="item">
				<view class="label">电话</view>
				<input :value="form.mobile" @input="Onmobile" placeholder="输入联系人电话" type="number" placeholder-class="placeholderClass" />
			</view>
		</view>
		<!-- 按钮 -->
		<view class="fix-bottom-btn flex-row-center">
			<view class="btn flex-row-center font-ffffff font15" @click="submit">保存</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				id: null,
				form: {
					name: '',
					mobile: '',
					type: 1,
				}
			}
		},
		onLoad(options) {
			if (options.id) {
				this.id = options.id
				this.$Http({
					url: 'editFriend',
					token: true,
					data: {
						id: options.id
					},
				}).then(res => {
					if (res.code == 1) {
						this.form.name = res.data.name
						this.form.mobile = res.data.mobile
					}
				});
			}
		},
		methods: {
			Onname(e) {
				this.form.name = e.detail.value
			},
			Onmobile(e) {
				this.form.mobile = e.detail.value
			},
			// 提交
			submit() {
				if (this.form.name == '') {
					uni.showToast({ title: '请输入您的姓名', icon: 'none', duration: 2000 })
					return
				}
				if (!this.$base.checkMobile(this.form.mobile)) {
					uni.showToast({ title: '请输入正确的手机号码', icon: 'none', duration: 2000 })
					return
				}
				if (this.id) {
					this.form.id = this.id
				}
				this.$Http({
					url: this.id ? 'editFriend' : 'addFriend',
					data: this.form,
					token: true,
					type: 'POST'
				}).then(res => {
					if (res.code == 1) {
						uni.showToast({ title: '操作成功' })
						setTimeout(()=>{
							uni.navigateBack({ delta: 1 });
						}, 2000)
					} else {
						uni.showToast({ title: res.msg, icon: 'none' })
					}
				});
			}
		}
	}
</script>

<style lang="less">
	@import url("../my/form.less");
</style>
